package com.example.exchange2.confing;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.security.PublicKey;

/**
 * ClassName:RabbitConfig
 * Package:com.example.ttl1.confing
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/8/11-21:21
 * @Version:v1.0
 */
@Configuration
public class RabbitConfig {

    @Value("${my.exchangeNormalName}")
    private String exchangeNormalName;

    @Value("${my.queueNormalName}")
    private String queueNormalName;

    @Value("${my.exchangeAlternateName}")
    private String exchangeAlternateName;

    @Value("${my.queueAlternateName}")
    private String queueAlternateName;



    @Bean
    public DirectExchange directNormalExchange() {
        return ExchangeBuilder
                .directExchange(exchangeNormalName)
                .alternate(exchangeAlternateName)//设置备用交换机
                .build();
    }

    /**
     * 备用交换机设置成扇形交换机 --不需要路由
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange(){
        return ExchangeBuilder.fanoutExchange(exchangeAlternateName).build();
    }

    @Bean
    public Queue queueNormal() {
        return QueueBuilder.durable(queueNormalName).build();
    }

    @Bean
    public Queue queueAlternate(){
        return QueueBuilder.durable(queueAlternateName).build();
    }

    @Bean
    public Binding binding1(DirectExchange directNormalExchange, Queue queueNormal) {
        return BindingBuilder.bind(queueNormal).to(directNormalExchange).with("info");
    }

    @Bean
    public Binding binding2(FanoutExchange fanoutExchange,Queue queueAlternate){
        return BindingBuilder.bind(queueAlternate).to(fanoutExchange);
    }


}
